package main

import (
	"os"
	"os/signal"

	"cheater_zero/ScreenWatcher"
	log "cheater_zero/pkg/logging"
	"cheater_zero/routers"
	"fmt"
	"net/http"

	"context"
	"time"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

// @title Swagger Example API12223
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host 127.0.0.1:8000
func main() {
	ScreenWatcher.Init()

	//初始化目录结构
	_ = os.MkdirAll("log", os.ModePerm)

	router := routers.InitRouter()
	//swagger api文档的路由, 需basic鉴权
	router.GET(
		"/docs/*any",
		gin.BasicAuth(gin.Accounts{
			"admin":                   "123456",
			"dtmonitor_cluster_agent": "mymm84785836",
			"zcloud":                  "mymm84785836",
		}),
		ginSwagger.DisablingWrapHandler(swaggerFiles.Handler, "ENV_DISABLE_SWAGGER"),
	)

	s := &http.Server{
		Addr:           fmt.Sprintf(":%d", 8000),
		Handler:        router,
		ReadTimeout:    60 * time.Second,
		WriteTimeout:   60 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	go func() {
		if err := s.ListenAndServe(); err != nil {
			log.DefaultLogger.Debugf("Listen: %s\n", err)
		}
	}()

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt)
	<-quit

	log.DefaultLogger.Errorf("Shutdown Server ...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := s.Shutdown(ctx); err != nil {
		log.DefaultLogger.Errorf("Server Shutdown:", err)
	}

	log.DefaultLogger.Infof("Server exiting")
}
